﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
public partial class Manage_NewPictureUpdate : System.Web.UI.Page
{
    public string strName1, strContent1;
    public string news_id;
    static string  _DownLoad;
    static string _IsShow;
    static string _fileDownLoad;

    protected void Page_Load(object sender, EventArgs e)
    {
        string strName, strContent;
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAdmin);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            if (!IsPostBack)
            {
                string strSql;
                if (Request.QueryString["news_id"] != null)
                    news_id = SysFun.IsSQL(Request.QueryString["news_id"].ToString());
                else
                    return;
                strSql = "select * from newsPicture where news_id=" + news_id;
                Db DbInfo = new Db();
                System.Data.IDataReader dr;
                dr = DbInfo.DataReader(strSql);
                if (dr.Read())
                {
                    //txtTitle.Text = dr["Title"].ToString();
                    //txtContent.Text = dr["Content"].ToString();
                    txtAccountId.Text = dr["AccountId_add"].ToString();
                    txtAccountId.Visible = false;
                    txtAddDate.Text = Convert.ToDateTime(dr["AddDate"].ToString()).ToShortDateString();
                    txtInfoId.Text = dr["news_id"].ToString();
                    txtInfoId.Visible = false;
                    strName1 = dr["news_title"].ToString();
                    strContent1 = dr["news_Content"].ToString();
                    _DownLoad= dr["DownLoad"].ToString();
                    _IsShow = dr["IsShow"].ToString();
                    _fileDownLoad = dr["DownLoadfile"].ToString();
                }
                dr.Close();
                dr.Dispose();
                DbInfo.Close();
            }
            strName = SysFun.ToTrim(Request.Form["txtName"]);
            strContent = SysFun.ToTrim(Request.Form["txtContentHtml"]);
            if (strName != "" && strContent != "")
            {
                HttpFileCollection files = HttpContext.Current.Request.Files;
               
                string LoadFileName = "";
                string fileName = "";
                string fileExtension = "";
                Boolean Useful = false;
                for (int iFile = 0; iFile < files.Count; iFile++)
                {
                    ///'检查文件扩展名字

                    HttpPostedFile postedFile = files[iFile];

                    fileName = FileUpload1.FileName;
                    LoadFileName = "";
                    fileExtension = System.IO.Path.GetExtension(fileName);
                    if (((fileExtension.ToUpper()) == ".JPG") || (fileExtension.ToUpper() == ".JPEG"))
                    {
                        // System.Drawing.Image image = System.Drawing.Image.FromFile(postedFile.FileName);
                        //if (image.Width > 186 || image.Height > 141)
                        // {
                        //     Response.Write("<script>alert('图片太大，请按要求大小上传！')</scirpt>");
                        //      Useful = false;
                        //  }
                        //  else
                        //  {
                        Useful = true;
                        //  }
                        //  image.Dispose();   
                    }
                    else
                    {
                        Useful = false;
                    }
                    if (fileName != "" && Useful)
                    {
                        //LoadFileName = fileName;
                        LoadFileName = SysFun.GetIntDate() + "-" + SysFun.GetIntTime() + "-" + SysFun.GetRnd(10000, 99999);



                        ///注意：可能要修改你的文件夹的匿名写入权限。

                        postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("../UploadFile/") + LoadFileName + fileExtension);


                    }
                }
                if (fileName != "" && Useful)
                {
                    Db db1 = new Db();
                    Grove.ORM.ObjectOperator oo = db1.GetObjectOperator();
                    HZ.MBSM.Model.newsPicture newsPicture = new HZ.MBSM.Model.newsPicture();
                    newsPicture.news_id = Convert.ToInt64(txtInfoId.Text);
                    newsPicture.news_title = strName;
                    //dr["Content"].ToString().Replace("\r\n","<br>")
                    newsPicture.news_Content = strContent.Replace("\r\n", "");
                    newsPicture.AccountId_add = Convert.ToInt64(txtAccountId.Text);
                    newsPicture.AddDate = Convert.ToDateTime(txtAddDate.Text);
                    if (_DownLoad != "")
                    {
                        System.IO.File.Delete(Server.MapPath(_DownLoad));  //删除不用图片
                    }
                    newsPicture.DownLoad = newsPicture.DownLoad = LoadFileName + fileExtension;
                    oo.Update(newsPicture);
                    oo.Dispose();
                    db1.Close();
                    Response.Write("<script>alert('保存成功！！！');</script>");
                    HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "newsPicture", newsPicture.news_id + "|" + newsPicture.news_title + "|" + newsPicture.news_Content + "|" + newsPicture.AddDate, "../Magage/newsPictureUpdagte.aspx");

                    Response.Write("<script>window.opener.location.reload();window.close();</script>");

                }
                else if (fileName == "")
                {
                    Db db1 = new Db();
                    Grove.ORM.ObjectOperator oo = db1.GetObjectOperator();
                    HZ.MBSM.Model.newsPicture newsPicture = new HZ.MBSM.Model.newsPicture();
                    newsPicture.news_id = Convert.ToInt64(txtInfoId.Text);
                    newsPicture.news_title = strName;
                    //dr["Content"].ToString().Replace("\r\n","<br>")
                    newsPicture.news_Content = strContent.Replace("\r\n", "");
                    newsPicture.AccountId_add = Convert.ToInt64(txtAccountId.Text);
                    newsPicture.AddDate = Convert.ToDateTime(txtAddDate.Text);
                    newsPicture.DownLoad = _DownLoad;
                    newsPicture.DownLoadfile = _fileDownLoad;
                    newsPicture.IsShow = Convert.ToInt32(_IsShow);
                   
                    oo.Update(newsPicture);
                    oo.Dispose();
                    db1.Close();
                    Response.Write("<script>alert('保存成功！！！');</script>");

                    Response.Write("<script>window.opener.location.reload();window.close();</script>");
                }
                else
                {
                    strStatus.ForeColor = System.Drawing.Color.Red;
                    strStatus.Text = "只能上传图片文件格式(.JPG 或 .JPEG　)!";


                }
               
            }
        }
    }
}
